package com.aplink.dellcommercial.fragments;

import android.os.Bundle;
import android.os.Message;

import com.aplink.dellcommercial.R;
import com.aplink.dellcommercial.database.DellDataTable;
import com.aplink.dellcommercial.webservices.DataResult;
import com.aplink.dellcommercial.webservices.Methods;
import com.aplink.dellcommercial.webservices.WebServiceHelper;
import com.aplink.generic.GenericFragment;
import com.aplink.generic.constant.HandlerConstant;
import com.aplink.generic.dialog.GenericAlertDialog;
import com.aplink.generic.handler.IHandleMessage;
import com.aplink.generic.pojo.GenericObject;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.pojo.controlspec.MenuSpec.MenuItem;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;

public class QuizFragment extends GenericFragment implements IHandleMessage {

    @Override
    protected void onHandleControlClickEvent( ControlSpec controlSpec ) {

    }

    @Override
    public int getContentView() {
        return R.layout.aplink_dellcommercial_quiz_activity;
    }

    @Override
    public int getFormDesign() {
        return R.raw.form_design_quiz_activity;
    }

    @Override
    public int getContentData() {
        return R.raw.content_data_quiz_activity;
    }

    @Override
    public String getTable() {
        return DellDataTable.Quiz.TABLE_NAME;
    }

    @Override
    public void onActivityCreated( Bundle savedInstanceState ) {
        super.onActivityCreated( savedInstanceState );
        this.initData();
        this.loadDataToForm();
        this.getHandler().setHandleMessage( this );
        sortQuiz();
    }

    @SuppressWarnings("unchecked")
    @Override
    public void onHandleMessage( Message msg ) {
        if ( msg.what == HandlerConstant.HTTP_TASK_RESULT ) {
            DataResult dataResult = (DataResult) msg.obj;
            if ( dataResult.getMethodName().equals( Methods.GET_ALL_QUIZ ) ) {
                ArrayList<GenericObject> allQuiz = (ArrayList<GenericObject>) dataResult
                        .getData();
                ArrayList<Long> totalRowInserted = insertCommand().insertList(
                        allQuiz, getObjPropCol() );
                if ( totalRowInserted.size() > 0 ) {
                    this.loadDataToForm();
                    sortQuiz();
                } else {
                    GenericAlertDialog.newInfoDialog( getActivity(),
                            getString( R.string.dell__latest_tip ),
                            getString( R.string.dell__dialog_info ) ).show();
                }
            }
        }
    }

    @Override
    protected void onHandleOptionMenuClickEvent( MenuItem menuItem ) {
        String validationGroup = menuItem.getValidationgroup();
        if ( validationGroup.equalsIgnoreCase( "get_lastest_quiz" ) ) {
            WebServiceHelper.getInstance().getAllQuiz( getActivity(),
                    getHandler(), getObjPropList() );
        }
    }

    private void sortQuiz() {
        getCustomListView().sort( mComparator );
    }

    Comparator<GenericObject> mComparator = new Comparator<GenericObject>() {
        @Override
        public int compare( GenericObject paramT1, GenericObject paramT2 ) {
            Date dateParamT1 = (Date) paramT1
                    .getValueFromAttributeName( "CloseDate" );
            Date dateParamT2 = (Date) paramT2
                    .getValueFromAttributeName( "CloseDate" );
            return dateParamT2.compareTo( dateParamT1 );
        }
    };
}
